package com.ftg.learn.rabbitmq;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ftg.learn.entity.LearnStudentEntity;
import com.ftg.learn.mapper.LearnStuRedisMapper;
import com.ftg.learn.util.SmsUtil;
import jakarta.annotation.Resource;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * org.springframework.amqp.core.Queue  队列的信息
 *
 * @author 布衣
 */
@Configuration
public class RabbitConfig {

    @Resource
    ObjectMapper objectMapper;

    @Resource
    private LearnStuRedisMapper learnStuRedisMapper;

    @Bean
    public Queue queue() {
        return new Queue("stu_add_queue", false, false, false);
    }

    @Bean
    public Queue smsQueue() {
        return new Queue("verfiy_code_sms", false, false, false);
    }

    @RabbitListener(queues = "verfiy_code_sms")
    public void receiveSms(String message) {
        SmsUtil.sendSms(message, "1234");
    }

    @RabbitListener(queues = "stu_add_queue")
    public void receive(String message) throws JsonProcessingException {
        HashMap<String, String> map = objectMapper.readValue(message, HashMap.class);
        System.out.println("接收到的参数：" + map);

        LearnStudentEntity learnStudentEntity = objectMapper.readValue(map.get("parameter"), LearnStudentEntity.class);
        System.out.println("接收到的参数：" + learnStudentEntity);
        switch (map.get("operation").toString()) {
            case "add":
                learnStuRedisMapper.add(learnStudentEntity);
                break;
            case "modify":
                learnStuRedisMapper.modify(learnStudentEntity);
                break;
        }

        System.out.println("接收到消息：" + message);
    }

}
